-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: allow to run preflight validation only #14744
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
That way you can e.g. run preflight on each keystroke and full validation only on blur
🦋 Changeset detectedLatest commit: 7368c91 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
packages/kit/src/exports/public.d.ts
Outdated
/** Set this to `true` to also show validation issues of fields that haven't been touched yet. */ | ||
includeUntouched?: boolean; | ||
/** Set this to `true` to only run the `preflight` validation. */ | ||
clientOnly?: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this, so it's explicitly linked to .preflight(...)
?
clientOnly?: boolean; | |
preflightOnly?: boolean; |
What would this look like in practice? <script>
import { form } from "$lib/remote/forms.remote.ts";
import { schema } from "$lib/schema";
function validate() {
form.preflight(schema).validate({ preflightOnly: true }); // runs preflight schema
form.validate(); // runs server schema
}
</script>
<form {...form}></form> or <script>
import { form } from "$lib/remote/forms.remote.ts";
import { schema } from "$lib/schema";
function validate() {
form.validate(); // runs preflight schema
form.validate({ preflightOnly: false }); // skips preflight, runs server schema
}
</script>
<form {...form.preflight(schema)}></form> |
The preflight schema would always be used. This just allows you to skip server validation. So you could for example do preflight validation on every keystroke, but additionally run server validation on <form
{...myform.preflight(schema)}
oninput={() => myform.validate({ preflightOnly: true })}
onchange={() => myform.validate()}
>...</form> |
One thing I don't love about this is that server issues get nuked as soon as preflight-only validation succeeds. We can fix that with a simple change... -const is_server_validation = !validated?.issues;
+const is_server_validation = !validated?.issues && !preflightOnly; ...but then we have another problem: preflight validation issues will be added to server validation issues, instead of replacing them, and it looks really weird. Fixing that is slightly trickier. I've opened #14759 to address it. |
I don't sure I understand - I do want to have the server validation issues persisted when a preflight validation fails. Why wouldn't I? There's no world in which a server validation can be duplicate to a preflight validation, because then the server validation wouldn't have ran in the first place. Having a server validation issue ripped out under my fingers while typing is never a good thing IMO. And IIRC we agreed that we don't want to have them go away, so the simple fix should be enough? |
Consider a case similar to the one in the test case I added in #14759 — on the server you have this schema... v.object({
a: v.pipe(v.string(), v.minLength(3, 'a is too short'), v.maxLength(7, 'a is too long')),
b: v.pipe(v.string(), v.minLength(3, 'b is too short')),
c: v.pipe(v.string(), v.minLength(3, 'c is too short'))
}) ...and on the client you have this: v.object({
a: v.pipe(v.string(), v.maxLength(7, 'a is too long')),
b: v.string(),
c: v.string()
}) This is contrived, but totally valid — the preflight constraints are a subset of the server-side constraints. If you submit this with empty inputs, preflight validation will succeed and you'll be left with the following:
Currently on this branch, as soon as you start typing into the
|
* preserve server issues on preflight validation, unless there are newer preflight-only issues * sort correctly * no longer necessary * lint * Update packages/kit/src/runtime/client/remote-functions/form.svelte.js Co-authored-by: Simon H <[email protected]> --------- Co-authored-by: Simon H <[email protected]>
That way you can e.g. run preflight on each keystroke and full validation only on blur
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.